package com.example.drools.controller;

import com.example.drools.dto.RiskAssessmentRequest;
import com.example.drools.dto.SimpleRiskRequest;
import com.example.drools.model.*;
import com.example.drools.service.RiskControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 风控控制器
 */
@RestController
@RequestMapping("/api/risk")
public class RiskControlController {

    @Autowired
    private RiskControlService riskControlService;

    /**
     * 风控评估接口 - 简化版本用于前端
     */
    @PostMapping("/assess")
    public List<RiskAssessment> assessRisk(@RequestBody SimpleRiskRequest request) {
        System.out.println("收到风控评估请求: " + request.getUserId());
        System.out.println("信用分: " + request.getCreditScore());
        System.out.println("收入: " + request.getIncome());
        System.out.println("贷款金额: " + request.getLoanAmount());
        System.out.println("就业年限: " + request.getEmploymentYears());
        
        UserProfile userProfile = new UserProfile();
        userProfile.setId(request.getUserId());
        userProfile.setSegments(new HashSet<>());
        userProfile.setRegion("北京"); // 默认地区

        Context context = new Context();
        context.setPage("风控评估");
        context.setDevice("WEB");

        // 创建模拟分数数据
        List<Score> scores = List.of(
            new Score(request.getUserId(), "creditScore", request.getCreditScore()),
            new Score(request.getUserId(), "income", request.getIncome()),
            new Score(request.getUserId(), "loanAmount", request.getLoanAmount()),
            new Score(request.getUserId(), "employmentYears", (double)request.getEmploymentYears())
        );

        List<RiskAssessment> result = riskControlService.assessRisk(userProfile, context, scores);
        System.out.println("控制器返回结果数量: " + result.size());
        return result;
    }

    /**
     * 风控评估接口 - 完整版本
     */
    @PostMapping("/assess-full")
    public List<RiskAssessment> assessRiskFull(@RequestBody RiskAssessmentRequest request) {
        UserProfile userProfile = new UserProfile();
        userProfile.setId(request.getUserId());
        userProfile.setAge(request.getAge());
        userProfile.setSegments(new HashSet<>(request.getSegments()));
        userProfile.setRegion(request.getRegion());

        Context context = new Context();
        context.setPage(request.getPage());
        context.setDevice(request.getDevice());

        return riskControlService.assessRisk(userProfile, context, request.getScores());
    }

    /**
     * 快速风控评估接口
     */
    @PostMapping("/quick-assess")
    public RiskAssessment quickAssess(@RequestParam String userId,
                                    @RequestParam String userType,
                                    @RequestParam double riskScore,
                                    @RequestParam(defaultValue = "未知") String location) {
        return riskControlService.quickAssess(userId, userType, riskScore, location);
    }

    /**
     * 批量风控评估接口
     */
    @PostMapping("/batch-assess")
    public List<RiskAssessment> batchAssess(@RequestBody List<UserProfile> userProfiles,
                                          @RequestParam(defaultValue = "批量检查") String page) {
        Context context = new Context();
        context.setPage(page);
        context.setDevice("API");

        return riskControlService.batchAssess(userProfiles, context);
    }

    /**
     * 获取用户风控状态
     */
    @GetMapping("/status/{userId}")
    public RiskAssessment getRiskStatus(@PathVariable String userId,
                                      @RequestParam(defaultValue = "普通用户") String userType) {
        // 生成随机风险分数用于演示
        double riskScore = Math.random() * 100;
        return riskControlService.quickAssess(userId, userType, riskScore, "北京");
    }
}